package online.flowerinsnow.minecraft.bukkit.testffa.manager;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SQLManager {
    private SQLManager() {
    }

    private static Connection connection;

    public static void loadDriver() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static @Nullable Connection connect(String ip, int port, String user, String password) {
        loadDriver();
        try {
            return connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + port + "/test?user=" + user +
                    "&password=" + password + "&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false");
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Connection getConnection() {
        return connection;
    }
}
